package com.atgugui.eduservice.controller;


import com.atgugui.commonutils.Result;
import com.atgugui.eduservice.entity.EduChapter;
import com.atgugui.eduservice.entity.chapter.ChapterVo;
import com.atgugui.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-29
 */
@Api(tags = "课程章节控制层")
@RestController
@RequestMapping("/eduservice/edu-chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @ApiOperation(value = "根据id查询课程大纲列表信息")
    @GetMapping("/getChapterVideo/{courseId}")
    public Result getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return Result.ok().data("allChapterVideo",list);
    }

    @ApiOperation(value = "添加章节信息")
    @PostMapping("/addChapter")
    public Result addChapter(@RequestBody EduChapter eduChapter) {
        boolean save = eduChapterService.save(eduChapter);
        return save ? Result.ok() : Result.error();
    }

    @ApiOperation(value = "根据id查询章节信息")
    @GetMapping("/getChapterInfoById/{chapterId}")
    public Result getChapterInfoById(@PathVariable String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return Result.ok().data("chapterInfo",eduChapter);
    }

    @ApiOperation(value = "更新章节信息")
    @PostMapping("/updateChapter")
    public Result updateChapter(@RequestBody EduChapter eduChapter) {
        boolean b = eduChapterService.updateById(eduChapter);
        return b ? Result.ok() : Result.error();
    }

    /**
     * 删除章节信息：如果该章节有子章节，无法删除
     * @return
     */
    @ApiOperation(value = "删除章节信息")
    @DeleteMapping("/deleteChapter/{chapterId}")
    public Result deleteChpater(@PathVariable String chapterId) {
        boolean delete = eduChapterService.deleteChapter(chapterId);
        return delete ? Result.ok() : Result.error();
    }
}

